<#
 #   It is recommended to test the script on a local machine for its purpose and effects. 
 #   ManageEngine Endpoint Central will not be responsible for any 
 #   damage/loss to the data/setup based on the behavior of the script.

 #   Description: Script is designed to search for all files on a machine based on their file extension
 #   Parameters:".txt"

 #   Remarks: The script has to be deployed as Computer Configuration
 #   Configuration Type - Computer
#>

# Set the extension to search
$extension = $args[0]

# Get all logical disks on the machine
$drives = Get-WmiObject -Class Win32_LogicalDisk | Where-Object { $_.DriveType -eq 3 }

# Flag to check if any matching files are found
$filesFound = $false

# Iterate through each drive and search for files with the extension
foreach ($drive in $drives) {
    $rootDirectory = $drive.DeviceID + "\"

    # Get all files with the specified extension in the current drive, including hidden files
    $files = Get-ChildItem -Path $rootDirectory -Filter "*$extension" -Recurse -File -Force -ErrorAction SilentlyContinue

    # Display the matching files
    if ($files.Count -gt 0) {
        $files | ForEach-Object {
            Write-Host "File Name: $($_.Name)"
            Write-Host "File Path: $($_.FullName)"
            Write-Host "File Size: $($_.Length) bytes"
        }

        # Set the flag to true as matching files are found
        $filesFound = $true
    }
}

# Search in the AppData folder
$appDataPath = [Environment]::GetFolderPath('ApplicationData')

# Get all files with the specified extension in the AppData folder and its subdirectories, including hidden files
$appDataFiles = Get-ChildItem -Path $appDataPath -Filter "*$extension" -Recurse -File -Force -ErrorAction SilentlyContinue

# Display the matching files in the AppData folder
if ($appDataFiles.Count -gt 0) {
    $appDataFiles | ForEach-Object {
        Write-Host "File Name: $($_.Name)"
        Write-Host "File Path: $($_.FullName)"
        Write-Host "File Size: $($_.Length) bytes"
    }

    # Set the flag to true as matching files are found
    $filesFound = $true
}

# Display a message if no matching files are found
if (-not $filesFound) {
    Write-Host "No files with the extension $extension found."
}